package com.WhatsUp.activity;

import com.WhatsUp.tools.Attendance;
import com.WhatsUp.tools.FileHelper;
import com.WhatsUp.tools.ServerHelper;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public abstract class FatherActivity extends Activity {
	public ServerHelper server = ServerHelper.getInstant();

	private String IP = "172.22.161.35";
	private String dir = "ceshi/whatsup";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initialize();
		Attendance.getInstant();
		initialize(savedInstanceState);
	}
	
	@Override
	protected void onDestroy(){
		try{
			Attendance.getInstant().cancel();
		}
		catch(Exception e){
			return ;
		}
		super.onDestroy();
	}

	public abstract void initialize(Bundle savedInstanceState);
	
	private String IPFile = "ipconfig.con";
	private void initialize() {
		String setting = FileHelper.ReadSettings(this, IPFile);
		if (setting != null) {
			String data[] = setting.split("#");
			if(data.length == 2){
				this.IP = data[0];
				this.dir = data[1];
				Log.i("TEST", "IP"+IP);
				Log.i("TEST", "dir"+dir);
			}
		}
		server.setDirectory(this.dir);
		server.setIP(this.IP);
		if(server.IsConnected()){
			FileHelper.WriteSettings(this, IPFile, this.IP+"#"+this.dir);
			Log.i("TEST", "Connected!");
		}
	}
}
